<?php
namespace Mobile\Controller;
use Think\Controller;
class ClassesController extends Controller 
{
	//手机分类显示
    public function index()
    {
        //首页显示城市
        $city = I('get.newcity')?I('get.newcity'):'广州';
        $this->assign('city',$city);
        //授课城市显示
        $citys = M('city')->where('parent_id=0')->select();
        $this->assign('citys',$citys);


		//获取年级选择
		$grade = I('get.grade')?I('get.grade'):'';
    	$this->assign('grade',$grade);
    	//年级显示
    	$nianji = M('grade')->where('parent_id=0')->select();
    	$this->assign('nianji',$nianji);

		//获取科目选择
		$subject = I('get.subject')?I('get.subject'):'';
    	$this->assign('subject',$subject);
        //课程科目显示
        $kecheng = M('subjects')->select();
        $this->assign('kecheng',$kecheng);

		//获取区域选择
		$area = I('get.area')?I('get.area'):'';
    	$this->assign('area',$area);
        //区域显示
        $cityid = M('city')->where(array('city_name'=>$city))->getField('cityid');
        $city_qiyu = M('city')->where(array('parent_id'=>$cityid))->select();
        $this->assign('city_qiyu',$city_qiyu);

         //获取年级id
        $gradeid = M('grade')->where(array('grade'=>$grade))->getField('gradeid');

        //判断是否关键字搜索
        $keyword = I('get.keyword');
        if (!empty($keyword)) {
            //判断是否为数字   
            if (is_numeric($keyword )) {
                $sql = "select t1.id,t1.name,t1.face,t2.jiaoling,t3.grades,t3.price,t3.subject from laoshi_user as t1 inner join jiaoling_jli as t2 on t1.id=t2.uid inner join showke_info as t3 on t1.id=t3.uid where is_shenhe_xs=1 and is_dongjie=1 and is_del=0 and t1.city='{$city}' and t1.id='{$keyword}'";
            }else {
                $sql = "select t1.id,t1.name,t1.face,t2.jiaoling,t3.grades,t3.price,t3.subject from laoshi_user as t1 inner join jiaoling_jli as t2 on t1.id=t2.uid inner join showke_info as t3 on t1.id=t3.uid where is_shenhe_xs=1 and is_dongjie=1 and is_del=0 and t1.city='{$city}' and t1.name='{$keyword}'"; 
            }

        }else {
            if (!empty($grade) && empty($subject) && empty($area)) {
    			$sql = "select t1.id,t1.name,t1.face,t2.jiaoling,t3.grades,t3.price,t3.subject from laoshi_user as t1 inner join jiaoling_jli as t2 on t1.id=t2.uid inner join showke_info as t3 on t1.id=t3.uid where is_shenhe_xs=1 and is_dongjie=1 and is_del=0 and t1.city='{$city}' and FIND_IN_SET('{$gradeid}',t3.grades)";
            }elseif (empty($grade) && !empty($subject) && empty($area)) {
            	$sql = "select t1.id,t1.name,t1.face,t2.jiaoling,t3.grades,t3.price,t3.subject from laoshi_user as t1 inner join jiaoling_jli as t2 on t1.id=t2.uid inner join showke_info as t3 on t1.id=t3.uid where is_shenhe_xs=1 and is_dongjie=1 and is_del=0 and t1.city='{$city}' and t3.subject='{$subject}'";
            }elseif (empty($grade) && empty($subject) && !empty($area)) {
            	$sql = "select t1.id,t1.name,t1.face,t2.jiaoling,t3.grades,t3.price,t3.subject from laoshi_user as t1 inner join jiaoling_jli as t2 on t1.id=t2.uid inner join showke_info as t3 on t1.id=t3.uid where is_shenhe_xs=1 and is_dongjie=1 and is_del=0 and t1.city='{$city}' and t3.sk_city='{$area}'";
            }elseif (!empty($grade) && !empty($subject) && empty($area)) {
            	$sql = "select t1.id,t1.name,t1.face,t2.jiaoling,t3.grades,t3.price,t3.subject from laoshi_user as t1 inner join jiaoling_jli as t2 on t1.id=t2.uid inner join showke_info as t3 on t1.id=t3.uid where is_shenhe_xs=1 and is_dongjie=1 and is_del=0 and t1.city='{$city}' and FIND_IN_SET('{$gradeid}',t3.grades) and t3.subject='{$subject}'";	
            }elseif (empty($grade) && !empty($subject) && !empty($area)) {
            	$sql = "select t1.id,t1.name,t1.face,t2.jiaoling,t3.grades,t3.price,t3.subject from laoshi_user as t1 inner join jiaoling_jli as t2 on t1.id=t2.uid inner join showke_info as t3 on t1.id=t3.uid where is_shenhe_xs=1 and is_dongjie=1 and is_del=0 and t1.city='{$city}' and t3.subject='{$subject}' and t3.sk_city='{$area}'";	
            }elseif (!empty($grade) && !empty($subject) && !empty($area)) {
            	$sql = "select t1.id,t1.name,t1.face,t2.jiaoling,t3.grades,t3.price,t3.subject from laoshi_user as t1 inner join jiaoling_jli as t2 on t1.id=t2.uid inner join showke_info as t3 on t1.id=t3.uid where is_shenhe_xs=1 and is_dongjie=1 and is_del=0 and t1.city='{$city}' and FIND_IN_SET('{$gradeid}',t3.grades) and t3.subject='{$subject}' and t3.sk_city='{$area}'";	
            }else {
            	$sql = "select t1.id,t1.name,t1.face,t2.jiaoling,t3.grades,t3.price,t3.subject from laoshi_user as t1 inner join jiaoling_jli as t2 on t1.id=t2.uid inner join showke_info as t3 on t1.id=t3.uid where is_shenhe_xs=1 and is_dongjie=1 and is_del=0 and t1.city='{$city}'";
            }
        }
        $chaxuns = M()->query($sql);
		$this->assign('chaxuns',$chaxuns);// 赋值数据集

        //授课年级 跟上面字段(1,2,3,4) 匹配对应ID跟name显示
        $grade_ones = M('grade')->where('parent_id=0')->select();
        $this->assign('grade_ones',$grade_ones);
    	
        $this->display();
    }

















}